<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>商品添加页面</title>
    <%@ include file="/page/menu/head.jsp" %>
    <script>
        function imgPreview(dom){
            var reader=new FileReader();
            var file=dom.files[0];
            var imageType=/^image\//;
            if(!imageType.test(file.type)){
                alert("请上传图片！");
                return;
            }
            reader.onload=function (e){
                var img=document.getElementById("preview");
                img.src=e.target.result;
            };
            reader.readAsDataURL(file);
        }
    </script>
    <script>
        function imgPreview1(dom){
            var reader=new FileReader();
            var file=dom.files[0];
            var imageType=/^image\//;
            if(!imageType.test(file.type)){
                alert("请上传图片！");
                return;
            }
            reader.onload=function (e){
                var img=document.getElementById("preview1");
                img.src=e.target.result;
            };
            reader.readAsDataURL(file);
        }
    </script>
</head>
<body>
    <div id="header">
        <span class="wel_word">商品添加页面</span>
        <%@ include file="/page/menu/sale_menu.jsp"%>
        <div id="main">
            <c:if test="${ empty param.id}">
            <form action="sale/ItemServlet?action=add" method="post" enctype="multipart/form-data">
                <input type="hidden" name="pageNo" value="${param.pageNo}">
                <input type="hidden" name="id" value="${ requestScope.item.id }" />
                <table>
                    <tr>
                        <td>名称</td>
                        <td>价格</td>
                        <td>销量</td>
                        <td>库存</td>
                        <td>图片</td>
                        <td colspan="2">操作</td>
                    </tr>
                    <tr>
                        <td><input name="name" type="text" value="${requestScope.item.name}"/></td>
                        <td><input name="price" type="text" value="${requestScope.item.price}"/></td>
                        <td><input name="sales" type="text" value="${requestScope.item.sales}"/></td>
                        <td><input name="stock" type="text" value="${requestScope.item.stock}"/></td>
                        <td><div class="image">
                            <img id="preview" width="50" height="50" src="${requestScope.item.imgPath}">
                            <input type="file" name="file" onchange="imgPreview(this)" accept="image/*" />
                        </div></td>
                        <td><input type="hidden" name="img_path" value="${requestScope.item.imgPath}"></td>
                        <td><input type="submit" value="提交"/></td>
                    </tr>
                </table>
            </form>
            </c:if>
            <c:if test="${ not empty param.id}">
            <form action="sale/ItemServlet?action=update" method="post" enctype="multipart/form-data">

                <input type="hidden" name="pageNo" value="${param.pageNo}">
                <input type="hidden" name="id" value="${ requestScope.item.id }" />
                <table>
                    <tr>
                        <td>名称</td>
                        <td>价格</td>
                        <td>销量</td>
                        <td>库存</td>
                        <td>图片</td>
                        <td colspan="2">操作</td>
                    </tr>
                    <tr>
                        <td><input name="name" type="text" value="${requestScope.item.name}"/></td>
                        <td><input name="price" type="text" value="${requestScope.item.price}"/></td>
                        <td><input name="sales" type="text" value="${requestScope.item.sales}"/></td>
                        <td><input name="stock" type="text" value="${requestScope.item.stock}"/></td>
                        <td><div class="image">
                            <img id="preview1" width="50" height="50" src="${requestScope.item.imgPath}">
                            <input type="file" name="file" onchange="imgPreview1(this)" accept="image/*" />
                        </div></td>
                        <td><input type="submit" value="提交"/></td>
                    </tr>
                </table>
            </form>
            </c:if>
        </div>
    </div>
</body>
</html>
